package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DBOperation.DBOperation;

/**
 * Servlet implementation class GetCommentServlet
 */
@WebServlet("/GetCommentServlet")
public class GetCommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetCommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int fid = Integer.parseInt(request.getParameter("fid"));
		System.out.println("--servlet--fid--"+fid);
		DBOperation dbo = new DBOperation();
		ResultSet rs = dbo.getAllComments(fid);
		
		String commentJson = "";
		try {
			while(rs.next())
			{
				String username = rs.getString("name");
				String company = rs.getString("company");
				String content = rs.getString("content");
				Timestamp timeStamp = rs.getTimestamp("gmt_create");
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time = df.format(timeStamp);
				
				String[] arrKey = {"username", "company", "content", "gmt_create"};
				String[] arrValue = {username, company, content, time};
				commentJson += dbo.getJson(arrKey, arrValue) + ";";
				System.out.println(commentJson);
			}
			if(commentJson.length() > 0)
				commentJson = commentJson.substring(0, commentJson.length()-1);
			System.out.println("-feedJson-"+commentJson);
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			out.print(commentJson);
			out.flush();
			out.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("--异常--");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
